package com.itany.corejava.code15_stream;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年08月09日14:02
 */
public class Test01_创建stream对象 {
    public static void main(String[] args) {

        // 方式一：Collection体系的集合
        List<Integer> list= Arrays.asList(1,3,54,98,78,65);
        Stream<Integer> stream=list.stream();
        System.out.println(stream);

        // stream.forEach(new Consumer<Integer>() {
        //     @Override
        //     public void accept(Integer integer) {
        //         System.out.println(integer);
        //     }
        // });
        // stream.forEach(integer -> System.out.println(integer));
        stream.forEach(System.out::println);
        stream.close();

        //方式二:Map体系的集合
        Map<Integer,String> map=new HashMap<>();
        map.put(1,"admin");
        map.put(2,"jack");
        map.put(3,"alice");

        Stream<Integer> keyStream=map.keySet().stream();
        keyStream.forEach(System.out::println);
        System.out.println("----------------");
        Stream<Map.Entry<Integer,String>> entryStream=map.entrySet().stream();
        entryStream.forEach(System.out::println);

        //数组类型
        Integer[] arr={10,20,30,40};
        //通过Arrays工具类获取
        Stream<Integer> s1=Arrays.stream(arr);
        s1.forEach(System.out::println);

        //通过Stream的静态方法进行获取
        String[] arr2={"南京","北京","上海","南通","徐州","连云港"};
        Stream<String> s2=Stream.of(arr2);
        s2.forEach(System.out::println);

        //可变长类型
        Stream<String> s3=Stream.of("李白","杜甫","李商隐","李龟年");
        s3.forEach(System.out::println);
        System.out.println("------------------------");
        //无限流，将无限个元素添加到Stream中，不停止
        UnaryOperator<Integer> operator=UnaryOperator.identity();

        //参数一：初始元素
        // 参数二:直接使用UnaryOperator对象，表示每一次都是同一个值
        // Stream<Integer> s4=Stream.iterate(1,operator);
        // s4.forEach(System.out::println);

        UnaryOperator<Integer> operator2=new UnaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer) {
                return integer+1;
            }
        };

        // Stream<Integer> s5=Stream.iterate(1,operator2);
        Stream<Integer> s5=Stream.iterate(1,i->i+1);
        s5.forEach(System.out::println);
    }
}
